package com.cjg.netCode;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;

public class fileNet {
    public static void main(String[] args) throws Exception {

        //创建一个管道绝对流向哪里
        Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 8887);
        //创建一个通道 决定 输出 内容
        OutputStream os = socket.getOutputStream();
        //读取要输出的内容 转存到 通道中
        FileInputStream fileInputStream = new FileInputStream("修改mysql密码.png");
        //写出文件
        byte[] bytes = new byte[1024];
        int lent;
        while ((lent=fileInputStream.read(bytes))!=-1){
            os.write(bytes,0,lent);
        }
        //通知服务器我已经传输完了
        socket.shutdownOutput();


        //确定服务器接收完毕,才能断开

        InputStream inputStream = socket.getInputStream();
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        byte[] bytes1 = new byte[1024];
        int lent1;
        while ((lent1=inputStream.read(bytes1))!=-1){
            byteArrayOutputStream.write(bytes,0,lent1);
        }
        System.out.println(byteArrayOutputStream.toString(StandardCharsets.UTF_8));

        byteArrayOutputStream.close();
        inputStream.close();

        fileInputStream.close();
        socket.close();

    }
}
